<template>
  <el-container class="mainbox">
    <el-header>
      <h3>欢迎 {{ myInfo._02 }} {{ myInfo._01 }}</h3>
      <el-dialog :close-on-click-modal="false" width="40%" title="个人信息" :visible.sync="showInfo">
        <el-form :model="myInfo">
          <el-form-item label="姓名">
            <el-input v-model="myInfo._01"></el-input>
          </el-form-item>
          <el-form-item label="学院">
            <el-input v-model="myInfo._02"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer">
          <el-button @click="showInfo = false">取 消</el-button>
          <el-button type="primary" @click="updateUserInfo">保存</el-button>
        </div>
      </el-dialog>
      <div>
        <el-button size="small" v-if="!isAdmin" type="primary" icon="el-icon-plus" @click="newRow">新增</el-button>
        <el-button size="small" type="primary" icon="el-icon-download" @click="showExport">导出</el-button>
        <el-button size="small" type="primary" icon="el-icon-s-tools" @click="showInfo = true">设置个人信息</el-button>
        <el-button size="small" type="danger" icon="el-icon-switch-button" @click="outLogin">退出</el-button>
      </div>
    </el-header>
    <el-container class="containbox">
      <el-aside style="overflow-y: hidden" width="100px">
        <el-menu :default-active="currentItem.firstValue" @select="menuSelect">
          <el-menu-item v-for="item in docList" :key="item.firstValue" :index="item.firstValue">
            <i class="el-icon-document"></i>
            <span slot="title">{{ item.firstValue }}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view
          @selectItem="selectItems = $event"
          :_tabledata="selectItems"
          v-bind="currentItem"
          @doc="getDoc"
          :token="token"
          ref="tableSheet"
        ></router-view>
        <el-backtop target=".el-main" :visibility-height="50"></el-backtop>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
let BaaS = window.BaaS;
import $ from "jquery";
import { $isLogin } from "@/assets/crud";
export default {
  //import引入的组件需要注入到对象中才能使用
  components: {},
  data() {
    return {
      token: "",
      isAdmin: false,
      myInfo: {},
      showInfo: false,
      selectItems: [],
      docList: [
        {
          // 主题
          firstValue: "论文",
          // 预览的主界面表头
          secondValue: [
            {
              name: "全部作者",
              type: "a",
              width: "180px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "论文题目",
              type: "_02",
              width: "350px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "发表时间",
              type: "_04",
              width: "90px",
              sort: true,
              fi: "filters",
              mo: "filter-method",
              filter: "b",
              fang: () => false,
            },
            {
              name: "发表刊物/论文集",
              type: "_03",
              width: "150px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "刊物类型",
              type: "d",
              width: "120px",
              sort: false,
              fi: "filters",
              mo: "filter-method",
              guo: [
                { text: "SCI(一区)", value: "SCI(一区)" },
                { text: "SCI(二区)", value: "SCI(二区)" },
                { text: "SCI(三区)", value: "SCI(三区)" },
                { text: "SCI(四区)", value: "SCI(四区)" },
                { text: "SCI(未分区)", value: "SCI(未分区)" },
                { text: "EI", value: "EI" },
                { text: "CSCD", value: "CSCD" },
                { text: "中文核心", value: "中文核心" },
                { text: "普刊", value: "普刊" },
                { text: "会议论文", value: "会议论文" },
              ],
              fang: function(value, row) {
                return row.d.includes(value);
              },
            },
            {
              name: "审核状态",
              type: "status",
              sort: false,
              fi: "null",
              mo: "null",
            },
          ],
          // 表格导出，完整项
          thirdValue: [
            { key: "a", label: "全部作者", disabled: false },
            { key: "_02", label: "论文题目", disabled: false },
            { key: "_03", label: "发表刊物/论文集", disabled: false },
            { key: "_04", label: "发表/出版时间(完整)", disabled: false },
            { key: "b", label: "发表/出版时间(年)", disabled: false },
            { key: "c", label: "发表/出版时间(月)", disabled: false },
            { key: "d", label: "刊物类型", disabled: false },
            { key: "_06", label: "卷(期):页码", disabled: false },
            { key: "_07", label: "ISSN号", disabled: false },
            { key: "_08", label: "DOI码", disabled: false },
          ],
          fourthValue: [
            { key: "${index+1}", label: "序号", disabled: true },
            { key: "${obj.a}", label: "全部作者", disabled: true },
            { key: "${obj._02}", label: "论文题目", disabled: true },
            { key: "${obj._03}", label: "发表刊物/论文集", disabled: true },
            { key: "${obj.b}", label: "发表/出版时间(年)", disabled: true },
            { key: "${obj.c}", label: "发表/出版时间(月)", disabled: false },
            { key: "${obj.d}", label: "刊物类型", disabled: false },
            { key: "${obj._06}", label: "卷(期):页码", disabled: true },
            { key: "${obj._07}", label: "ISSN号", disabled: false },
            { key: "${obj._08}", label: "DOI码", disabled: false },
          ],
          // 初始文本导出项
          fifthValue: [
            { key: "[", label: "[" },
            { key: "${index+1}", label: "序号", disabled: true },
            { key: "]", label: "]" },
            { key: "${obj.a}", label: "全部作者", disabled: true },
            { key: "${obj._02}", label: "论文题目", disabled: true },
            { key: "[J]", label: "[J]" },
            { key: ",", label: "," },
            { key: "${obj._03}", label: "发表刊物/论文集", disabled: true },
            { key: ",", label: "," },
            { key: "${obj.b}", label: "发表/出版时间(年)", disabled: true },
            { key: ",", label: "," },
            { key: "${obj._06}", label: "卷(期):页码", disabled: true },
            { key: ".", label: "." },
          ],
        },
        {
          firstValue: "专利",
          secondValue: [
            {
              name: "专利申请者",
              type: "a",
              width: "180px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "专利题名",
              type: "_02",
              width: "350px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "授权日期",
              type: "_05",
              width: "120px",
              sort: true,
              fi: "filters",
              mo: "filter-method",
              filter: "b",
              fang: () => false,
            },
            {
              name: "专利类型",
              type: "_07",
              width: "150px",
              sort: true,
              fi: "filters",
              mo: "filter-method",
              filter: "_07",
              fang: () => false,
            },
            {
              name: "授权情况",
              type: "_06",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "审核状态",
              type: "status",
              sort: false,
              fi: "null",
              mo: "null",
            },
          ],
          thirdValue: [
            { key: "a", label: "专利申请者", disabled: false },
            { key: "_02", label: "专利题名", disabled: false },
            { key: "_03", label: "专利国别", disabled: false },
            { key: "_04", label: "专利号", disabled: false },
            { key: "_05", label: "授权日期", disabled: false },
            { key: "_06", label: "授权情况", disabled: false },
            { key: "_07", label: "专利类型", disabled: false },
          ],
          fourthValue: [
            { key: "${index+1}", label: "序号", disabled: true },
            { key: "${obj.a}", label: "专利申请者", disabled: true },
            { key: "${obj._02}", label: "专利题名", disabled: true },
            { key: "${obj._03}", label: "专利国别", disabled: true },
            { key: "${obj._04}", label: "专利号", disabled: true },
            { key: "${obj._05}", label: "授权日期", disabled: true },
            { key: "${obj._06}", label: "授权情况", disabled: true },
            { key: "${obj._07}", label: "专利类型", disabled: false },
          ],
          // 初始文本导出项
          fifthValue: [
            { key: "[", label: "[" },
            { key: "${index+1}", label: "序号", disabled: true },
            { key: "]", label: "]" },
            { key: "${obj.a}", label: "专利申请者", disabled: true },
            { key: "${obj._02}", label: "专利题名", disabled: true },
            { key: "[P]", label: "[P]" },
            { key: ",", label: "," },
            { key: "${obj._05}", label: "授权日期", disabled: true },
            { key: ",", label: "," },
            { key: "${obj._03}", label: "专利国别", disabled: true },
            { key: ",", label: "," },
            { key: "${obj._04}", label: "专利号", disabled: true },
            { key: ".", label: "." },
            { key: "${obj._06}", label: "授权情况", disabled: true },
            { key: "。", label: "。" },
          ],
        },
        // { firstValue: "软著", secondValue: "" },
        {
          firstValue: "项目",
          secondValue: [
            {
              name: "项目名称",
              type: "_01",
              width: "260px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "项目来源",
              type: "_03",
              width: "200px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "项目成员",
              type: "d",
              width: "230px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "主持人单位",
              type: "_05",
              width: "200px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "项目经费(万)",
              type: "b",
              width: "100px",
              sort: true,
              fi: "null",
              mo: "null",
              filter: "b",
            },
            {
              name: "到账经费(万)",
              type: "c",
              width: "100px",
              sort: true,
              fi: "null",
              mo: "null",
              filter: "c",
            },
            {
              name: "级别",
              type: "_09",
              width: "120px",
              sort: false,
              fi: "filters",
              mo: "filter-method",
              filter: "_09",
              guo: [
                { text: "国家级", value: "国家级" },
                { text: "省级", value: "省级" },
                { text: "市级", value: "市级" },
                { text: "校级", value: "校级" },
                { text: "其他", value: "其他" },
              ],
              fang: () => false,
            },
            {
              name: "审核状态",
              type: "status",
              sort: false,
              fi: "null",
              mo: "null",
            },
          ],
          thirdValue: [
            { key: "_01", label: "项目名称", disabled: false },
            { key: "_02", label: "项目编号", disabled: false },
            { key: "_03", label: "项目来源", disabled: false },
            { key: "d", label: "项目成员", disabled: false },
            { key: "_05", label: "主持人单位", disabled: false },
            { key: "b", label: "项目经费(万)", disabled: false },
            { key: "c", label: "到账经费(万)", disabled: false },
            { key: "a", label: "项目期限", disabled: false },
            { key: "_09", label: "级别", disabled: false },
            { key: "_10", label: "主持情况", disabled: false },
            { key: "_11", label: "课题进展情况", disabled: false },
          ],
          fourthValue: [
            { key: "${index+1}", label: "序号", disabled: true },
            { key: "${obj._01}", label: "项目名称", disabled: true },
            { key: "${obj._02}", label: "项目编号", disabled: true },
            { key: "${obj._03}", label: "项目来源", disabled: true },
            { key: "${obj.d}", label: "项目成员", disabled: false },
            { key: "${obj._05}", label: "主持人单位", disabled: false },
            { key: "${obj._06}", label: "项目经费(万)", disabled: true },
            { key: "${obj._07}", label: "到账经费(万)", disabled: true },
            { key: "${obj.a}", label: "项目期限", disabled: false },
            { key: "${obj._09}", label: "级别", disabled: false },
            { key: "${obj._10}", label: "主持情况", disabled: true },
            { key: "${obj._11}", label: "课题进展情况", disabled: true },
          ],
          // 初始文本导出项
          fifthValue: [
            { key: "[", label: "[" },
            { key: "${index+1}", label: "序号", disabled: true },
            { key: "]", label: "]" },
            { key: "${obj._01}", label: "项目名称", disabled: true },
            { key: "（", label: "（" },
            { key: "${obj._02}", label: "项目编号", disabled: true },
            { key: "），", label: "），" },
            { key: "${obj._03}", label: "项目来源", disabled: true },
            { key: "，", label: "，" },
            { key: "${obj._06}", label: "项目经费(万)", disabled: true },
            { key: "${obj._07}", label: "到账经费(万)", disabled: true },
            { key: "万，", label: "万，" },
            { key: "${obj._10}", label: "主持情况", disabled: true },
            { key: "，", label: "，" },
            { key: "${obj._11}", label: "课题进展情况", disabled: true },
            { key: "。", label: "。" },
          ],
        },
        {
          firstValue: "获奖",
          secondValue: [
            {
              name: "完成人",
              type: "a",
              width: "280px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "成果名称",
              type: "_02",
              width: "280px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "成果类型",
              type: "_03",
              width: "200px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "颁奖单位",
              type: "_05",
              width: "180px",
              sort: false,
              fi: "null",
              mo: "null",
            },
            {
              name: "获奖时间",
              type: "_06",
              width: "120px",
              sort: true,
              fi: "filters",
              mo: "filter-method",
              filter: "b",
              fang: () => false,
            },
            {
              name: "审核状态",
              type: "status",
              sort: false,
              fi: "null",
              mo: "null",
            },
          ],
          thirdValue: [
            { key: "a", label: "主要完成人", disabled: false },
            { key: "_07", label: "指导老师", disabled: false },
            { key: "_02", label: "成果名称", disabled: false },
            { key: "_03", label: "成果类型", disabled: false },
            { key: "_04", label: "获奖等级", disabled: false },
            { key: "_05", label: "颁奖单位", disabled: false },
            { key: "_06", label: "获奖时间", disabled: false },
          ],
          fourthValue: [
            { key: "${index+1}", label: "序号", disabled: true },
            { key: "${obj.a}", label: "主要完成人", disabled: true },
            { key: "${obj._07}", label: "指导老师", disabled: false },
            { key: "${obj._02}", label: "成果名称", disabled: true },
            { key: "${obj._03}", label: "成果类型", disabled: true },
            { key: "${obj._04}", label: "获奖等级", disabled: true },
            { key: "${obj._05}", label: "颁奖单位", disabled: true },
            { key: "${obj._06}", label: "获奖时间", disabled: true },
          ],
          // 初始文本导出项
          fifthValue: [
            { key: "[", label: "[" },
            { key: "${index+1}", label: "序号", disabled: true },
            { key: "]", label: "]" },
            { key: "${obj._03}", label: "成果类型", disabled: true },
            { key: "${obj._02}", label: "成果名称", disabled: true },
            { key: "获", label: "获" },
            { key: "${obj._05}", label: "颁奖单位", disabled: true },
            { key: "${obj._04}", label: "获奖等级", disabled: true },
            { key: "，主要完成人", label: "，主要完成人" },
            { key: "${obj.a}", label: "主要完成人", disabled: true },
            { key: "，获奖时间：", label: "，获奖时间：" },
            { key: "${obj._06}", label: "获奖时间", disabled: true },
          ],
        },
      ],
      currentItem: {},
    };
  },
  //监听属性
  computed: {},
  //监控data中的数据变化
  watch: {},
  //方法集合
  methods: {
    // 获取当前用户基本信息
    getUserInfo() {
      BaaS.auth
        .getCurrentUser()
        .then((user) => {
          this.myInfo = JSON.parse(user.get("info"));
        })
        .catch(() => {
          this.$message.error("获取基本信息失败，请刷新");
        });
    },
    updateUserInfo() {
      BaaS.auth
        .getCurrentUser()
        .then((user) => {
          return user.set("info", JSON.stringify(this.myInfo)).update();
        })
        .then(() => {
          this.getUserInfo();
          this.showInfo = false;
        })
        .catch(() => {
          this.$message.error("修改基本信息失败，请重试");
        });
    },
    // 退出登录，清除token
    outLogin() {
      BaaS.auth.logout().then(() => {
        this.$message.success("已退出！");
        window.$login = false;
        this.$router.replace("/");
      });
    },
    newRow() {
      // 到时候补充
      switch (this.currentItem.firstValue) {
        case "论文":
          this.$router.push("/home/paper");
          break;
        case "专利":
          this.$router.push("/home/patent");
          break;
        case "项目":
          this.$router.push("/home/project");
          break;
        case "获奖":
          this.$router.push("/home/awarded");
          break;
        default:
          break;
      }
    },
    showExport() {
      if (this.selectItems.length) {
        this.$router.push("/home/export");
      } else {
        this.$message({
          showClose: true,
          message: "请选择要导出的项目",
          type: "warning",
        });
      }
    },
    // 选择某个菜单项。
    menuSelect(key) {
      this.deleteHigh();
      this.currentItem = this.docList.find((i) => {
        return i.firstValue == key;
      });
      this.$router.push("/home");
      this.selectItems = "";
      if (this.$refs.tableSheet.noMore != undefined) {
        this.$refs.tableSheet.noMore = false;
      }
    },
    highLight() {
      setTimeout(() => {
        let i = $(".el-menu-item.is-active");
        i.css("backgroundColor", "#ecf5ff");
      }, 150);
    },
    deleteHigh() {
      let i = $(".el-menu-item.is-active");
      i.css("backgroundColor", "");
    },
    getDoc(data) {
      if (data) {
        this.currentItem = this.docList.find((s) => s.firstValue == data);
      }
    },
    createDir() {
      $.get(`http://120.78.68.201:3000/create/id_${BaaS.storage.get("uid")}`)
        .done((res) => {
          this.token = res.data.token;
        })
        .fail(() => {
          this.$message.error("文件接口凭证获取失败，如不使用文件上传功能，可忽略");
        });
    },
  },
  //生命周期 - 创建完成（可以访问当前this实例）
  created() {
    this.currentItem = this.docList[0];
    this.getUserInfo();
    this.isAdmin = BaaS.storage.get("ok");
  },
  //生命周期 - 挂载完成（可以访问DOM元素）
  mounted() {
    this.createDir();
    this.highLight();
  },
  beforeRouteEnter(to, from, next) {
    $isLogin(next);
  },
};
</script>
<style lang="less" scoped>
.mainbox {
  height: 100vh;
}
.containbox {
  overflow: auto;
}
.el-header {
  color: #333;
  border-bottom: solid 1px #e6e6e6;
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.el-aside {
  color: #333;
  border-right: solid 1px #e6e6e6;
  .el-menu {
    border: 0;
  }
}

.el-main {
  color: #333;
}
</style>
